﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YJKRevitParapet.Controller;

namespace YJKRevitParapet.Forms
{
	public partial class EditingInterfaceForm : System.Windows.Forms.Form
	{
		public bool PickButton
		{
			get
			{
				return this.m_PickButton;
			}
			set
			{
				this.m_PickButton = value;
			}
		}

		public bool IsContinuous
		{
			get
			{
				return true;
			}
		}

		public EditingInterfaceForm.OperateType CurrentOperateType
		{
			get
			{
				return this.m_CurrentOperateType;
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			EditingInterfaceForm.SendMessage(CreateParapetController._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			EditingInterfaceForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			EditingInterfaceForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		public EditingInterfaceForm()
		{
			this.InitializeComponent();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "EditParapetBaselineForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_DrawLine;
				this.SetSelectType(this.m_CurrentOperateType);
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			EditingInterfaceForm.m_Version = Convert.ToInt32(streamReader.ReadLine());
			if (EditingInterfaceForm.m_Version == 1)
			{
				this.m_CurrentOperateType = (EditingInterfaceForm.OperateType)Convert.ToInt32(streamReader.ReadLine());
				this.SetSelectType(this.m_CurrentOperateType);
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SetSelectType(EditingInterfaceForm.OperateType optType)
		{
			if (optType == EditingInterfaceForm.OperateType.OT_DrawLine)
			{
				this.tsbDrawLine.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawLine.CheckState = CheckState.Unchecked;
			}
			if (optType == EditingInterfaceForm.OperateType.OT_DrawRect)
			{
				this.tsbDrawRect.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawRect.CheckState = CheckState.Unchecked;
			}
			if (optType == EditingInterfaceForm.OperateType.OT_DrawArc3Pt)
			{
				this.tsbDrawArc3Pt.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
			}
			if (optType == EditingInterfaceForm.OperateType.OT_DrawArcCenter)
			{
				this.tsbDrawArcCenter.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
			}
			if (optType == EditingInterfaceForm.OperateType.OT_SelectRefCurve)
			{
				this.tsbSelectCurve.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbSelectCurve.CheckState = CheckState.Unchecked;
			}
			if (optType == EditingInterfaceForm.OperateType.OT_SelectRef)
			{
				this.tsbSelectRef.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbSelectRef.CheckState = CheckState.Unchecked;
			}
			if (optType == EditingInterfaceForm.OperateType.OT_Reverse)
			{
				this.tsbReverseOrientation.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbReverseOrientation.CheckState = CheckState.Unchecked;
			}
			if (optType == EditingInterfaceForm.OperateType.OT_Delete)
			{
				this.tsbDelete.CheckState = CheckState.Checked;
				return;
			}
			this.tsbDelete.CheckState = CheckState.Unchecked;
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "EditParapetBaselineForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(EditingInterfaceForm.m_Version);
			streamWriter.WriteLine((int)this.m_CurrentOperateType);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private void EditingInterfaceForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.label1.Text = "注：定位线为女儿墙面层外部";
			this.ReadData();
		}

		private void EditingInterfaceForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void EditingInterfaceForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetfocusMessage();
				return;
			}
			base.Focus();
		}

		private void tsbDrawLine_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_DrawLine;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawRect_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_DrawRect;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawArc3Pt_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_DrawArc3Pt;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawArcCenter_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_DrawArcCenter;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbSelectCurve_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_SelectRefCurve;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbSelectRef_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_SelectRef;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbReverseOrientation_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_Reverse;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDelete_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_Delete;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawLine_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawRect_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawArc3Pt_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawArcCenter_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbSelectCurve_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbSelectRef_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbReverseOrientation_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDelete_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void EditingInterfaceForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void EditingInterfaceForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (this.m_bIsSendEsc)
			{
				this.SendSetfocusMessage();
				this.SimulationPressESC();
			}
		}

		private static int m_Version = 1;

		private EditingInterfaceForm.OperateType m_CurrentOperateType = EditingInterfaceForm.OperateType.OT_DrawLine;

		private bool m_PickButton;

		public bool m_bIsSendEsc = true;

		public enum OperateType
		{
			OT_DrawLine = 1,
			OT_DrawRect,
			OT_DrawArc3Pt,
			OT_DrawArcCenter,
			OT_SelectRefCurve,
			OT_SelectRef,
			OT_Reverse,
			OT_Delete
		}
	}
}
